using System;
using NServiceBus;
using NServiceBus.Host;
using NServiceBus.Host.Profiles;
using NServiceBus.ObjectBuilder;
using NServiceBus.ObjectBuilder.StructureMap;
using StructureMap;
using StructureMap.Pipeline;

namespace LifecycleAwareness
{
    //public class EndpointConfigWithoutAwareness:IConfigureThisEndpoint,
    //    As.aServer,
    //    IDontWant.Sagas,
    //    ISpecify.ToRun<DemoEndpoint>,
    //    IWantCustomInitialization

    //{
    //    public void Init(Configure configure)
    //    {
    //        configure.Configurer.ConfigureComponent<SmtpGateway>(ComponentCallModelEnum.Singlecall);

    //    }
    //}

    //public class EndpointConfigWithAwareness : IConfigureThisEndpoint,
    //                                    As.aServer,
    //                                    IDontWant.Sagas,
    //                                    ISpecify.ToRun<DemoEndpoint>,
    //                                    IWantCustomInitialization,
    //                                    IHandleProfile<Integration> 
    //{
    //    private static bool IsInProductionMode = true;

    //    public void Init(Configure configure)
    //    {
    //        if (IsInProductionMode)
    //            configure.Configurer.ConfigureComponent<SmtpGateway>(ComponentCallModelEnum.Singlecall);
    //        else
    //            configure.Configurer.ConfigureComponent<FileLoggingEmailGateway>(ComponentCallModelEnum.Singlecall);
    //    }

    //    public void Init(IConfigureThisEndpoint specifier)
    //    {
    //        IsInProductionMode = false;
    //    }
    //}

    public class EndpointConfigUsingStructuremap : IConfigureThisEndpoint,
                                       As.aServer,
                                       ISpecify.ContainerTypeToUse<StructureMapObjectBuilder>,
                                       IDontWant.Sagas,
                                       ISpecify.ToRun<DemoEndpoint>,
                                       IHandleProfile<Integration>
    {
        private static readonly string IntegrationProfile = typeof (Integration).Name;

        public  EndpointConfigUsingStructuremap()
        {
            ObjectFactory.Configure(x =>
                                        {
                                            x.ForRequestedType<IEmailGateway>()
                                                .TheDefaultIsConcreteType<SmtpGateway>();
                                            
                                            //setup the alterations for the integraton profile
                                            x.CreateProfile(IntegrationProfile)
                                                .For<IEmailGateway>()
                                                    .UseConcreteType<FileLoggingEmailGateway >();
                                        });
        }

        public void Init(IConfigureThisEndpoint specifier)
        {
            //switch profile to "integration"
            ObjectFactory.Profile = IntegrationProfile;    
        }
    }

}